<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/25/21
 * Time: 3:26 PM
 */

namespace app\api\controller\v1;

// 裂变查询
use app\api\model\ProductOrder;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\StoreUserItem;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use app\api\model\Store as StoreModel;
use app\api\model\UserInvitation;
use app\api\service\Token as TokenService;

class Fission
{
    // 查询门店裂变数量
    public function getFissionByStore($start_time,$end_time)
    {
        $uid = TokenService::getCurrentUid();
        $user = UserModel::get($uid);
        $storeUser = StoreUserModel::where('user_id',$user['id'])->find();
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        if($user['type'] == 8) {
            $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
            $storeIds = array_column($storeUserItem,'store_id');
            $store = StoreModel::where('id','in',$storeIds)->order('id desc')->select()->toArray();
        } else {
            $store = StoreModel::order('id desc')->select()->toArray();
        }
        foreach ($store as $k=>$v){
            $whereOne = [];
            $whereOne[] = ['user.status','eq',3];
            $whereOne[] = ['user.type','eq',1];
            $whereOne[] = ['user.store_id', 'eq', $store[$k]['id']];
            $user = UserModel::hasWhere('invite',function($query){
                $query->where('UserInvitation.type','eq',1);
            })->where($whereOne)->select()->toArray();
            $fission = 0;
            foreach ($user as $ka=>$va){
                $whereTwo = $where;
                $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
                $whereTwo[] = ['status', 'gt', 1];
                $order = ProductOrder::where($whereTwo)->find();
                if($order) {
                    $fission = $fission +1;
                }
            }
            $store[$k]['fission'] = $fission;
        }
        if(count($store) > 0) {
            $store = arraySort($store,'fission');
        }
        return $store;
    }

    // 裂变查询订单列表
    public function getFissionByOrder($start_time,$end_time,$store_id)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $whereOne = [];
        $whereOne[] = ['user.status','eq',3];
        $whereOne[] = ['user.type','eq',1];
        $whereOne[] = ['user.store_id', 'eq', $store_id];
        $user = UserModel::hasWhere('invite',function($query){
            $query->where('UserInvitation.type','eq',1);
        })->where($whereOne)->select()->toArray();
        $array = [];
        foreach ($user as $ka=>$va){
            $whereTwo = $where;
            $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
            $whereTwo[] = ['status', 'gt', 1];
            $list = ProductOrder::where($whereTwo)->with('store,user')->select();
            if(!$list->isEmpty()) {
                foreach ($list->toArray() as $k=>$v){
                    $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
                    if($invite['type'] == 1) {
                        $list[$k]['invite'] = UserModel::get($invite['inviter']);
                    }elseif($invite['type'] == 2){
                        $store_user = StoreUserModel::get($invite['inviter']);
                        $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                        $list[$k]['invite']['nickname'] = $store_user['name'];
                        $list[$k]['invite']['phone'] = $store_user['phone'];
                    }elseif($invite['type'] == 3){
                        $team_user = TeamUserModel::get($invite['inviter']);
                        $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                        $list[$k]['invite']['nickname'] = $team_user['name'];
                        $list[$k]['invite']['phone'] = $team_user['phone'];
                    }
                    $list[$k]['pay_time_int'] = strtotime($list[$k]['pay_time']);
                    array_push($array,$list[$k]);
                }
            }
        }
        if(count($array) > 0) {
            $array = arraySort($array,'pay_time_int');
        }
        return $array;
    }
}